package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.VideoAuth;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.query.VideoAuthQueryObject;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.IVideoAuthService;
import cn.wolfcode.p2p.base.util.AjaxResult;
import cn.wolfcode.p2p.base.util.BitStateUtil;
import cn.wolfcode.p2p.base.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class VideoAuthController {

    @Autowired
    private IVideoAuthService videoAuthService;

    @Autowired
    private ILoginInfoService loginInfoService;

    @RequestMapping("/videoAuth")
    public String videoAuthPage(@ModelAttribute("qo") VideoAuthQueryObject qo, Model model) {
        model.addAttribute("pageInfo", videoAuthService.queryList(qo));
        return "videoAuth/list";
    }

    @RequestMapping("/videoAuthAuto")
    @ResponseBody
    public List<Map<String, Object>> videoAuthAuto(String username) {
        //前端用户员
        return loginInfoService.videoAuthAuto(username, LoginInfo.USERTYPE_NOMAL);
    }

    @RequestMapping("/videoAuth_audit")
    @ResponseBody
    public AjaxResult videoAuthAudit(Integer state, Long videoAuthId, String remark) {
        AjaxResult result = null;
        try {
            videoAuthService.audit(videoAuthId, state, remark);
            result = new AjaxResult("审核成功");
        } catch (DisplayableException e) {
            e.printStackTrace();
            //如果底层是抛出这个异常的话 则把 异常信息显示给用户看
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            //把异常信息写入到日志中
            result = new AjaxResult(false, "系统异常,程序员哥哥正在努力抢救中...");
        }
        return result;
    }
}
